#/*************************************************************************
#    > File Name: examples/example_MG.py
#    > Author: Yan Wang
#    > Mail: wangyan@imnu.edu.cn
#    > Created Time: 2022年06月29日 星期三 10时11分39秒
# ************************************************************************/
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append('../Lib/')
from Load_Lib import * 

print("The examples in this file are:")
ex_name={"1":"Split_Param_with_Num"
		,"2":"Generate_MG_ParamCard_from_DF"
		,"3":"Generate_MG_Scan_ParamCard_from_Dict"
		,"4":"Cover_Duplicated_File"
		,"5":"Tidy_MG_Run_Folder"
		,"6":"Tidy_MG_Events_Folder"
		,"7":"Move_MG_Event"
		,"8":"Move_MG_Event_MultiRun_for_one_BP"
		,"9":"Generate_MG_Events"
		,"10":"Extract_Scan_Xection"
		,"11":"Extract_One_Sentence_From_MultiFile"
		,"12":"Dataframe_One_with_Multi_Param_Files"
		,"13":"Compare_One_with_Multi_Param_DF"
		,"14":"Pack_Scan_Param"
		,"15":"Collect_MG_Cross"
		}        #字典，所有对应函数为Lib/MG_Operation.py


for key in ex_name:
	print(key, ex_name[key])    #if条件语句，如果key在ex_name里面，则输出eg：1 Split_Param_with_Num
num=input("请输入想要运行的例子：")

df_name="examples/test_input/data_grid_mh_mch.tsv"
df = pd.read_table(df_name)
source = './examples/test_input/param_card.dat'
node_num=3
# 为了简明起见， 从df中只取前五个数据， 分为3组
labels={"Block frblock":2,"23 9.118760e+01 # MZ ":4}


# Split_Param_with_Num
# 四个参数(A,B,C,D),A为输入的数据文件,B为替换列表,C最后输出文件,D为分数据的组数
# 将used_map里面的key分成node_num组并且转成list，传参到out_file
# 最后输出为：./examples/test_output/scan_param_Splite/param_card.dat文件
# 内容包括：
# {'mh': [[28.93582, 27.506943, 20.960417, 25.816828, 23.199866],(此外还有两组)}
# {'mch': [85.57786800000001, 93.766289, 106.37348600000001, 113.014924, 122.66396100000001],(此外还有两组)}
if num == "1" or num == ex_name["1"] or num == "all":
	used_map=["mh", "mch"]
	out_file='./examples/test_output/scan_param_Splite/param_card.dat'
	Exhibit_Function(Split_Param_with_Num, [df, used_map, out_file, node_num])


# Generate_MG_ParamCard_from_DF
# 注意这里replaced_content 和下面scan_param的不同， 这个函数每一个文件只针对一个参数， 因此replaced_content 中不包含scan的字样。
# 五个参数(A,B,C,D,E),A为输入的数据文件,B为需要被替换的文件,C为替换dict列表,D为被替换的系列数据,E为最后输出的一系列文件
# 将df中的系列数据替换到source中的replaced_content,且以com_file_name输出
# 最后输出为一系列文件,命名为：'./examples/test_output/scan_param_DF/param_card_{1、2...}.dat'
replaced_content={"tb":"      1 {} # TB",
	              "sba" :"    2 {} # sinbma",
		          "mh"  :"   25 {} # mh1",
			      "mH"  :"   35 {} # mh2",
				  "mA"  :"   36 {} # mh3",
				  "mch" :"   37 {} # mhc"}
com_file_name='./examples/test_output/scan_param_DF/param_card_{}.dat'


if num == "2" or num == ex_name["2"] or num == "all":
	Exhibit_Function(Generate_MG_ParamCard_from_DF, [df, source, replaced_content, labels, com_file_name])
	Exhibit_Function(Generate_MG_ParamCard_from_DF, [df, source, replaced_content, labels, com_file_name,[1,3,5]])



# Generate_MG_Scan_ParamCard_from_Dict
# 六个参数(A,B,C,D,E,F),A为被替换参数(是一个dict)的key,B为需要被替换的文件,C为替换dict列表,D为自动分组数,E为被替换的系列数据,F为最后输出文件
# 与以上函数类似的替换,只不过是将输入文件df自动分成了node_num组，此类替换带有’scanq1’字样
# 最后输出为一系列文件,命名为：'./examples/test_output/scan_param_Dict/param_card_{1、2...}.dat'
com_file_name='./examples/test_output/scan_param_Dict/param_card_{}.dat'
replaced_content={"tb":"    1 scan1: {} # TB",
	              "sba" :"    2 scan1: {} # sinbma",
		          "mh"  :"   25 scan1: {} # mh1",
			      "mH"  :"   35 scan1: {} # mh2",
				  "mA"  :"   36 scan1: {} # mh3",
				  "mch" :"   37 scan1: {} # mhc"}
if num == "3" or num == ex_name["3"] or num == "all":
       #将key单独提取为列表
	input_value_list=Get_Maplist_Key(replaced_content)   #查找replaced_content中的key
	input_dict=Make_DictList_from_DF(df, node_num, input_value_list, 5)   #将df中的input_value_list自动分成node_num组
	Exhibit_Function(Generate_MG_Scan_ParamCard_from_Dict, [input_dict, source, replaced_content, node_num, labels, com_file_name])


if num == "7" or num == ex_name["7"] or num == "all":
	#移动MG生成的事例文件到一个新的文件夹
	Exhibit_Function(Move_MG_Event, [original_folder, target_folder])


if num == "8" or num == ex_name["8"] or num == "all":
	#移动MG生成的事例文件到一个新的文件夹
	original_folder="/home/yancy/Software/MG5/Events_mh_mch_10"
	target_folder="/home/yancy/Software/ARCH_test/Auto_Running/auto_mab/Lib/examples/test_output/MG_folder/MG_Move/Events/"
	Create_Folder(target_folder,"/")
	#Exhibit_Function(Move_MG_Event_MultiRun_for_one_BP, [original_folder, target_folder])
	Move_MG_Event_MultiRun_for_one_BP(original_folder)
